package com.yoomiito.app.ui.my.psw;

import android.text.TextUtils;

import com.yoomiito.app.base.AppConfig;
import com.yoomiito.app.base.BaseLazyFragmentPresenter;
import com.yoomiito.app.net.YmApi;
import com.yoomiito.app.utils.YMToast;
import com.yoomiito.app.utils.encrypt.AESUtils;

import cn.droidlover.xdroidmvp.net.ApiException;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.BaseModel;
import cn.droidlover.xdroidmvp.net.XApi;

public class SetPayPswPresenter extends BaseLazyFragmentPresenter<SetPayPswFragment> {

    public SetPayPswPresenter(AppConfig appConfig) {
        super(appConfig);
    }

    public void setPayPsw(String psw){
        String ps = AESUtils.rsaEncrypt(psw);
        if (TextUtils.isEmpty(ps)){
            getV().setSuccess(false);
            YMToast.showText("密码设置失败，请重试");
            return;
        }
        YmApi.getYmService()
                .setPayPsw(ps)
                .compose(getV().bindToLifecycle())
                .compose(XApi.getApiTransformerNoData())
                .compose(XApi.getScheduler())
                .subscribe(new ApiSubscriber<BaseModel>(handleException()) {
                    @Override
                    public void onSuccess(BaseModel baseModel) {
                        getV().setSuccess(true);
                    }

                    @Override
                    public void onFail(ApiException error) {
                        getV().setSuccess(false);
                    }
                });
    }
}
